<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp" %>
<html>
<head>
    <title>代理商管理</title>
    <meta name="decorator" content="ani"/>
    <script type=text/javascript src="${ctxStatic}/ueditor/ueditor.config.js"></script>
    <script type=text/javascript src="${ctxStatic}/ueditor/ueditor.all.js"></script>
    <%@include file="/webpage/include/summernote.jsp" %>
    <style>
        .table {
            table-layout: fixed;
            position: relative;
            color: #666;
        }
        .table tr {
            height: 24px;
        }
        .table > caption + thead > tr:first-child > td, .table > caption + thead > tr:first-child > th, .table > colgroup + thead > tr:first-child > td, .table > colgroup + thead > tr:first-child > th, .table > thead:first-child > tr:first-child > td, .table > thead:first-child > tr:first-child > th {
            border: none;
            font-weight: normal;
            color: #999;
            border-top: 1px solid #efefef;
        }
        .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {
            border-top: 1px solid #efefef;
            vertical-align: middle;
        }
        .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
            position: relative;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
</head>
<body>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <a class="panelButton" href="${ctx}/web/wxAgents"><i class="ti-angle-left"></i> 返回</a>
                    </h3>
                </div>
                <div class="panel-body">
                    <form:form id="inputForm" modelAttribute="wxAgents" action="${ctx}/web/wxAgents/save" method="post"
                               class="form-horizontal">
                        <form:hidden path="id"/>
                        <form:hidden path="province"/>
                        <form:hidden path="city"/>
                        <form:hidden path="district"/>
                        <input id="config" name="config" type="hidden" />
                        <sys:message content="${message}"/>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><font color="red">*</font>代理商级别：</label>
                            <div class="col-sm-4">
                                <form:select path="agentLevel" items="${levelList}" itemLabel="agentType"
                                             cssClass="form-control"
                                             itemValue="agentLevel"></form:select>
                            </div>
                        </div>

                        <%-- <div class="col-xs-12 col-sm-6 col-md-4">
                        <label class="label-item single-overflow pull-left" title="上级代理商：">上级代理商：</label>
                        <sys:gridselect url="${ctx}/web/wxAgents/geAgentsByLevel/" id="parentId" name="parentId" value="id" labelName="userName" labelValue="userName"
                            title="选择上级代理商" cssClass="form-control required" fieldLabels="id|手机号" fieldKeys="id|userName" searchLabels="id|手机号" searchKeys="id|userName" ></sys:gridselect>
                    </div> --%>
                        <c:if test="${isAdd}">
                            <div class="form-group" id="parentAgentForm" style="display:none;">
                                <label class="col-sm-2 control-label"><font color="red">*</font>上级代理商：</label>
                                <div class="col-sm-4">
                                        <%-- <form:select path="parentId" items="${list}" itemLabel="userName" cssClass="form-control"
                                                     itemValue="id"></form:select> --%>

                                    <select id="parentId" name="parentId" class="form-control">
                                        <!-- <option value="1">13420902166</option>
                                        <option value="2">13420902177</option> -->
                                    </select>
                                </div>
                            </div>

                        </c:if>
                        <c:if test="${!isAdd}">
                            <div class="form-group" id="parentAgentForm">
                                <label class="col-sm-2 control-label"><font color="red">*</font>上级代理商：</label>
                                <div class="col-sm-4">
                                    <form:select path="parentId" items="${parentLevelWxAgents}" itemLabel="userName"
                                                 cssClass="form-control"
                                                 itemValue="id"></form:select>
                                </div>
                            </div>

                        </c:if>



                        <%--<div class="form-group">--%>
                        <%--<label class="col-sm-2 control-label">角色权限：</label>--%>
                        <%--<div class="col-sm-10">--%>
                        <%--<form:checkboxes path="roleIdList" items="${allRoles}"--%>
                        <%--itemLabel="name" itemValue="id" htmlEscape="false" cssClass="i-checks required"/>--%>
                        <%--</div>--%>
                        <%--</div>--%>

                        <div class="form-group">
                            <label class="col-sm-2 control-label"><font color="red">*</font>代理商账户：</label>
                            <div class="col-sm-10">
                                <form:input path="userName" htmlEscape="false" maxlength="30" minlength="1"
                                            placeholder="手机号" class="form-control required"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><font color="red">*</font>代理商密码：</label>
                            <div class="col-sm-10">
                                <form:input path="password" htmlEscape="false" maxlength="30" minlength="1"
                                            class="form-control required"/>
                            </div>
                        </div>
<%--                        <div class="form-group">--%>
<%--                            <label class="col-sm-2 control-label"><font color="red">*</font>代理商分销比例（百分比 如百分之1--%>
<%--                                则=1）：</label>--%>
<%--                            <div class="col-sm-10">--%>
<%--                                <form:input path="proportion" htmlEscape="false" maxlength="30" minlength="1"--%>
<%--                                            class="form-control required isIntGtZero"/>--%>
<%--                            </div>--%>
<%--                        </div>--%>
                        <div class="form-group" id="fenxiao" style="display: none">
                            <label class="col-sm-2 control-label"><font color="red">*</font>分销比例：</label>
                            <div class="col-sm-10">
                                <table class="table">
                                    <thead>
                                    <tr>
                                        <th style="width:80px">设备类型</th>
                                        <c:forEach items="${levelList}"  var ="item">
                                            <th style="width:80px">${item.agentType}</th>
                                        </c:forEach>
                                        <th style="width:180px">提现类型</th>

                                    </tr>
                                    </thead>
                                    <tbody id="type-items" class="ui-sortable" style="">
                                    <c:forEach items="${deviceType}"  var ="item">
                                        <tr class="tp_item">
                                            <td valign="top" class="must">
                                                <input type="text" value="${item.label}" data-code="${item.value}" readonly="" class="form-control">
                                            </td>
                                            <c:forEach items="${levelList}"  var ="l">
                                                <td valign="top" data-name="${l.agentLevel}">
                                                    <c:if test="${not empty commissionRatio}">
                                                        <input type="number" placeholder="请输入分销比例"
                                                               class="form-control valid" value="${commissionRatio[item.value][fn:trim(l.agentLevel)]}">
                                                    </c:if>
                                                    <c:if test="${empty commissionRatio}">
                                                        <input type="number" placeholder="请输入分销比例"
                                                               class="form-control valid" value="0">
                                                    </c:if>
                                                </td>
                                            </c:forEach>
                                            <td data-name="type">
<%--                                                <select name="guardian" class="form-control">--%>
<%--                                                    <option>立即</option>--%>
<%--                                                    <option>固定时间</option>--%>
<%--                                                </select>--%>
                                                <label class="radio-inline">
                                                    <c:if test="${not empty commissionRatio[item.value].type}">
                                                    <input type="radio" name="type${item.value}" ${commissionRatio[item.value].type=='1'?'checked':''} value="1"> 手动提现
                                                    </c:if>
                                                    <c:if test="${empty commissionRatio[item.value].type}">
                                                        <input type="radio" name="type${item.value}" value="1"> 手动提现
                                                    </c:if>
                                                </label>
                                                <label class="radio-inline">
                                                    <c:if test="${not empty commissionRatio[item.value].type}">
                                                        <input type="radio" name="type${item.value}" ${commissionRatio[item.value].type=='0'?'checked':''} value="0"> 定时提现
                                                    </c:if>
                                                    <c:if test="${empty commissionRatio[item.value].type}">
                                                        <input type="radio" name="type${item.value}" value="0"> 定时提现
                                                    </c:if>
                                                </label>
                                            </td>
                                        </tr>
                                    </c:forEach>

                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><font color="red">*</font>是否审核通过：</label>
                            <div class="col-sm-10">
                                <form:radiobuttons path="status" items="${fns:getDictList('yes_no')}" itemLabel="label"
                                                   itemValue="value" htmlEscape="false" class="i-checks required"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">合同内容：</label>
                            <div class="col-sm-10">
                                <input type="hidden" name="text"/>
                                <div id="contents">
                                    <textarea id=editor name="text" class="text"
                                              style="width: 100%; height: 260px;">${wxAgents.text}</textarea>
                                    <script type="text/javascript">
                                        var ue = UE.getEditor('editor', {
                                            elementPathEnabled: false,
                                            wordCount: false
                                        });
                                    </script>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><font color="red">*</font>省市区：</label>
                            <div class="col-sm-10">
                                <input id="city-picker1" readonly type="text" class="form-control required">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><font color="red">*</font>地址：</label>
                            <div class="col-sm-10">
                                <form:input path="address" htmlEscape="false" maxlength="100" minlength="1"
                                            placeholder="地址" class="form-control required"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">备注信息：</label>
                            <div class="col-sm-10">
                                <form:textarea path="remarks" htmlEscape="false" rows="4" class="form-control "/>
                            </div>
                        </div>
                        <c:if test="${fns:hasPermission('web:wxAgents:edit') || isAdd}">
                            <div class="col-lg-3"></div>
                            <div class="col-lg-6">
                                <div class="form-group text-center">
                                    <div>
                                        <button class="btn btn-primary btn-block btn-lg btn-parsley"
                                                data-loading-text="正在提交...">提 交
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </c:if>
                    </form:form>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    $(document).ready(function () {
        $("#city-picker1").citypicker({
            province: '${wxAgents.province}',
            city: '${wxAgents.city}',
            district: '${wxAgents.district}'
        });
        $("#inputForm").validate({
            submitHandler: function (form) {
                jp.loading();
                var cityPickerl = $("#city-picker1").val().split("/");

                if($("#agentLevel").val()==="3"){
                    //检查分销数据
                    let config=[];
                    $("#fenxiao tbody tr").each(function () {
                        let data ={};
                        $(this).find("td").each(function (i) {
                            if(i===0){
                                data.deviceType=$(this).find("input").data("code");
                            }else if($(this).data("name")==="type"){
                                let name = "type"+data.deviceType;
                                data[$(this).data("name")] = $(this).find("[name='"+name+"']:checked").val();
                            }else{
                                data[$(this).data("name")] = $(this).find("input").val();
                            }
                        });

                        config.push(data)
                    })
                    $("#config").val(JSON.stringify(config));
                }
                if (cityPickerl.length >=2) {
                    $("#province").val(cityPickerl[0]);
                    $("#city").val(cityPickerl[1]);
                    if(cityPickerl.length>2){
                        $("#district").val(cityPickerl[2]);
                    }

                    form.submit();
                } else {
                    jp.close();
                    $("#messageBox").text("请选择正确的省市区");
                }
            },
            errorPlacement: function (error, element) {
                $("#messageBox").text("输入有误，请先更正。");
                if (element.is(":checkbox") || element.is(":radio") || element.parent().is(".input-append")) {
                    error.appendTo(element.parent().parent());
                } else {
                    error.insertAfter(element);
                }
            }
        });
        $('#time').datetimepicker({
            format: "YYYY-MM-DD HH:mm:ss"
        });

        //切换代理商层级
        $("#agentLevel").change(function () {
            var $this = $(this);
            var level = $("#agentLevel").val();
            //总代理是公司时，无父级代理商

            if (level == 0) {
                $("#parentAgentForm").hide();
                $("#parentId").empty();
                return;
            }

            jp.loading();
            jp.get("${ctx}/web/wxAgents/geAgentsByLevel/" + level, function (data) {
                appendToSelect($("#parentId"), data.agentList);
                $("#parentAgentForm").show();
                jp.success('查询成功');
            })



            if(level==3){
                $("#fenxiao").show();
                $("#ywyWxAgents").show();
            }else{
                $("#fenxiao").hide();
                $("#ywyWxAgents").hide();
            }

        });
        if($("#agentLevel").val()==="3"){
            $("#fenxiao").show();
            $("#ywyWxAgents").show();
        }
        //代理商信息追加到下拉框
        function appendToSelect($select, data) {
            $select.html('');//清空
            var htmlStr = '';
            if (data == null || data.length == 0) {
                $select.html('<option value="-1">暂无可选数据</option>');
                return;
            }

            //代理商层级对应表
            var agentLevelType = {"0": "公司", "1": "一级代理商", "2": "二级代理","3": "门店", "4": "业务员"};

            //拼接信息
            //由于门店可以属于一级或二级代理商，所以将角色区分一下
            $.each(data, function (index, obj) {
                htmlStr += '<option value="' + obj.id + '">' + obj.userName + '(' + agentLevelType[obj.agentLevel] + ')</option>';
            });
            //添加到select下
            $select.html(htmlStr);
        }

    });
</script>
</body>
</html>
